package net.soevar.action;

import java.util.Properties;

import net.soevar.common.Common;
import net.soevar.database.hibernate.HibernateUtil;
import net.soevar.database.model.AthUser;
import net.soevar.email.sender.MailSender;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Caption;
import org.zkoss.zul.Center;
import org.zkoss.zul.Column;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.South;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

public class ForgotPasswordAction extends GenericAutowireComposer {
	private static final long serialVersionUID = -8856902256054261004L;
	private Textbox searchemail;
	private Window forgot_password;

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		// searchemail = (Textbox) comp.getFellow("searchemail");
		// System.out.println("searchemail.getValue() :" +
		// searchemail.getValue());

		// sendAndLoad(null);
	}

	public void onKirim(Event event) throws Exception {
		System.out.println("searchemail.getValue() :" + searchemail.getValue());
		try {
			sendAndLoad(searchemail.getValue());
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	public void onBack(Event event) throws Exception {
		// System.out.println("searchemail.getValue() :" +
		// searchemail.getValue());
		// this.self.detach();
		// page.getFirstRoot().detach();

	}

	public void sendAndLoad(String username) throws Exception {

		Session session = HibernateUtil.currentSession();
		AthUser user = (AthUser) session.createCriteria(AthUser.class)
				.add(Restrictions.eq("userId", username)).uniqueResult();
		// Mahasiswa mahasiswa = (Mahasiswa) session
		// .createCriteria(Mahasiswa.class)
		// .add(Restrictions.eq("nim", username)).uniqueResult();
		// Dosen dosen = user == null ? null : user.getDosen();
		// Pegawai pegawai = user == null ? null : user.getPegawai();

		final Properties properties = System.getProperties();

		String emailUser = null;
		String password;
		String passwordDecript;
		String subject = properties.getProperty("subject") == null ? "Pemberitahuan password untuk login ke Sistem Informasi Template"
				: properties.getProperty("subject");
		String body = null;
		String sender = properties.getProperty("email") == null ? "salmoedev@gmail.com"
				: properties.getProperty("email");

		// if (dosen != null) {
		// if (dosen.getEmail() == null) {
		// Messagebox
		// .show("Email anda belum terdaftar, silahkan hubungi admin untuk memasukkan email anda",
		// "Peringatan", Messagebox.OK, Messagebox.ERROR);
		// return;
		// }
		// emailUser = dosen.getEmail();
		// password = user.getUserPassword();
		// passwordDecript = Common.desEncrypter.decrypt(password);
		// body = "ID pengguna anda : " + user.getUserId()
		// + " . Kata sandi : " + passwordDecript;
		// } else if (pegawai != null) {
		// if (pegawai.getEmail() == null) {
		// Messagebox
		// .show("Email anda belum terdaftar, silahkan hubungi admin untuk memasukkan email anda",
		// "Peringatan", Messagebox.OK, Messagebox.ERROR);
		// return;
		// }
		// emailUser = pegawai.getEmail();
		// password = user.getUserPassword();
		// passwordDecript = Common.desEncrypter.decrypt(password);
		// body = "ID pengguna anda : " + user.getUserId()
		// + " . Kata sandi : " + passwordDecript;
		// } else if (user != null) {
		if (user != null) {
			if (user.getUserId() == null) {
				Messagebox
						.show("Email anda belum terdaftar, silahkan hubungi admin untuk memasukkan email anda",
								"Peringatan", Messagebox.OK, Messagebox.ERROR);
				return;
			}
			emailUser = user.getUserId();
			password = user.getUserPassword();
			passwordDecript = Common.desEncrypter.decrypt(password);
			body = "ID pengguna anda : " + user.getUserId()
					+ " . Kata sandi : " + passwordDecript;
		} else {
			Messagebox.show("Id pengguna tidak ditemukan", "Peringatan",
					Messagebox.OK, Messagebox.ERROR);
			return;

			// if (mahasiswa == null) {
			// Messagebox.show("Id pengguna tidak ditemukan", "Peringatan",
			// Messagebox.OK, Messagebox.ERROR);
			// return;
			// }
			// if (mahasiswa != null) {
			//
			// if (mahasiswa.getEmail() == null) {
			// Messagebox
			// .show("Email anda belum di terdaftar, silahkan hubungi admin untuk memasukkan email anda",
			// "Peringatan", Messagebox.OK,
			// Messagebox.ERROR);
			// return;
			// }
			// emailUser = mahasiswa.getEmail();
			// password = mahasiswa.getPass();
			// passwordDecript = Common.desEncrypter.decrypt(password);
			// body = "Username anda : " + mahasiswa.getNim()
			// + " . Password : " + passwordDecript;
			// }

		}

		MailSender.sendMail(subject, body, sender, emailUser);
		Messagebox.show("Password anda telah dikirim ke email anda ("
				+ emailUser + "), silahkan cek email anda", "Pemberitahuan",
				Messagebox.OK, Messagebox.INFORMATION);
	}
}